package net.wofly.sale.stock.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.wofly.right.domain.User;
import net.wofly.right.service.IUserService;
import net.wofly.sale.stock.domain.Warehouse;
import net.wofly.sale.stock.repository.WarehouseRepository;
import net.wofly.sale.stock.service.IWarehouseService;
import net.wofly.sale.stock.util.WarehouseType;

/**
 * 
 * 
 * @author liujianbo
 *
 */
@Service("iWarehouseService")
@Transactional
public class WarehouseServiceImpl implements IWarehouseService {
    
    public static final String EMPLOYEE_WAREHOUSE_NAME_SUFFIX = "的仓库";
    public static final String ORGANIZATION_WAREHOUSE_NAME_SUFFIX = "的仓库";
    
    @Autowired
    WarehouseRepository warehouseRepository;
    
    @Autowired
    IUserService iUserService;
    
    @Override
    public Warehouse save(Warehouse entity) {
        return warehouseRepository.save(entity);
    }
    
    @Override
    public void delete(Warehouse entity) {
        warehouseRepository.delete(entity);
    }
    
    @Override
    public void delete(String key) {
        warehouseRepository.delete(key);
    }
    
    @Override
    public Warehouse findOne(String key) {
        return warehouseRepository.findOne(key);
    }
    
    @Override
    public List<Warehouse> save(List<Warehouse> entityList) {
        return (List<Warehouse>) warehouseRepository.save(entityList);
    }
    
    @Override
    public List<Warehouse> findByWarehouseTypeAndStatus(String warehouseType, Integer status) {
        return warehouseRepository.findByWarehouseTypeAndStatus(warehouseType, status);
    }
    
    @Override
    public String createWareHouse(String warehouseID, WarehouseType warehouseType, String warehouseName) {
        Warehouse warehouse = new Warehouse();
        warehouse.setWarehouseID(warehouseID);
        warehouse.setStatus(1);
        warehouse.setWarehouseName(warehouseName);// 仓库名称如何设置
        warehouse.setWarehouseType(warehouseType.getValue().toString());
        warehouse.setModifyDate(System.currentTimeMillis());
        save(warehouse);
        return warehouseID;
    }
    
    @Override
    public Warehouse createPersonalWareHouse(String userID , String name) {
    		Warehouse warehouse = this.findOne(userID) ;
    		if (warehouse!=null) {
    			return warehouse ;
    		}
    		
    		warehouse = new Warehouse();
        warehouse.setWarehouseID(userID);
        warehouse.setStatus(1);
        warehouse.setWarehouseName(name + " 的仓库");// 仓库名称如何设置
        warehouse.setWarehouseType("3");
        warehouse.setModifyDate(System.currentTimeMillis());
        save(warehouse);
        return warehouse;
    }

	@Override
	public Warehouse getCompanyWarehouse() {
		List<Warehouse> companyWarehouses = this.warehouseRepository.findByWarehouseTypeAndStatus("1", 1) ;
		if (null == companyWarehouses && companyWarehouses.isEmpty()) {
			return null ;
		}
		
		return companyWarehouses.get(0);
	}

	@Override
	public Warehouse getOrgWarehouse(String orgID) {
		Warehouse warehouse = this.warehouseRepository.findOne(orgID) ;
		return warehouse ;
	}
}
